package com.hellobike.haxing.modules.supplier.task;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hellobike.haxing.common.constant.CacheConstant;
import com.hellobike.haxing.common.util.RedisUtil;
import com.hellobike.haxing.modules.order.listener.OrderSuccessListener;
import com.hellobike.haxing.modules.order.mapper.OrderMapper;
import com.hellobike.haxing.modules.order.service.IOrderService;
import com.hellobike.haxing.modules.purchase.entity.TPurchasePro;
import com.hellobike.haxing.modules.purchase.mapper.TPurchaseProMapper;
import com.hellobike.haxing.modules.purchase.service.ITPurchaseProService;
import com.hellobike.haxing.modules.supplier.dto.SkuNum;
import com.hellobike.haxing.modules.supplier.service.ITSupplierContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;

@Component
public class SupplierTask {

    @Autowired
    private ITSupplierContractService itSupplierContractService;

    @Autowired
    private ITPurchaseProService itPurchaseProService;


    @Resource
    private OrderMapper orderMapper;

    @Resource
    private OrderSuccessListener orderSuccessListener;

    @Autowired
    private IOrderService orderService;

    @Autowired
    private RedisUtil redisUtil;

    /**
     * 每天1点更新所有合同是否过期
     */
    @Scheduled(cron = "0 0 1 * * ?")
//    @Scheduled(cron = "0/59 * *  * * ? ")
    public void syncRecommendIndex() {
        itSupplierContractService.dealExpireContract();
    }

    /**
     * 每天23点生成所有po单
     */
    @Scheduled(cron = "0 10 22 * * ?")
//    @Scheduled(cron = "0/59 * *  * * ? ")
//    @Scheduled(fixedDelay = 2 * 60 * 1000)
    public void genAllPo() {
        itPurchaseProService.getAllPro();
    }

    @Scheduled(fixedDelay = 1 * 60 * 1000)
    public void fixedPro() {
        List<Long> orderIdList = orderMapper.selectFixedPoOrderId();
        if (!CollectionUtils.isEmpty(orderIdList)) {
            for (Long id : orderIdList) {
                orderService.updateSyncPurchase(id, 1);
                redisUtil.set(CacheConstant.ORADER_SUCCESS_MQ_ORDERID + id, id, CacheConstant.ONE_DAY);
            }
        }

    }

    @Autowired
    private TPurchaseProMapper tPurchaseProMapper;

//    @Scheduled(cron = "1 8 23 * * ?")
//    public void fixedPro1() {
//        List<SkuNum> skuNumList = orderMapper.selectSkuNum();
//        if (!CollectionUtils.isEmpty(skuNumList)) {
//            for (SkuNum skuNum : skuNumList) {
//                TPurchasePro pro = new TPurchasePro();
//                pro.setPurchaseVolume(skuNum.getC());
//                pro.setSkuId(skuNum.getSkuId());
//                tPurchaseProMapper.update(pro, new QueryWrapper<TPurchasePro>()
//                        .lambda().eq(TPurchasePro::getSkuId, skuNum.getSkuId()));
//            }
//        }
//    }


}
